package com.onpositive.wiki2.whole.model;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import com.onpositive.compactdata.StorableIntMap;

public class CategoryParser {

	static int pcount = 0;
	static int ecount = 0;
	static long charRead = 0;

	public static void main(String[] args) {
		try {
			File file = new File("C:/work/content");
			final StorableIntMap s = new StorableIntMap(new File(file,
					"idToIndex.map"));
			DataOutputStream ll = new DataOutputStream(
					new BufferedOutputStream(new FileOutputStream(new File(
							file, "relation.dat")),1024*1024));
			ContentAccess cs = new ContentAccess(file.getAbsolutePath());
			final SimpleRelationStore ra = new SimpleRelationStore(cs, ll);
			BufferedReader r = new BufferedReader(new InputStreamReader(
					new FileInputStream("C:/work/content/1.sql"), "UTF-8"));
			StringBuilder bld = new StringBuilder();
			int usedCount = cs.tb.usedCount;

			while (true) {
				int read = r.read();
				if (read == -1) {
					break;
				} else {
					bld.append((char) read);
				}
				if (bld.toString().contains("VALUES")) {
					break;
				}
			}
			boolean q = false;
			char pc = 0;
			char ppc = 0;
			String mprev=null;
			int escapeCount=0;
			while (true) {
				int read = r.read();
				charRead++;
				if (charRead % 10000000 == 0) {
					System.out.println(charRead / (1000 * 1000L));
				}
				if (read == -1) {
					break;
				} else {

					char c = (char) read;
					if (c == '(' && !q) {
						mprev=bld.toString();
						bld = new StringBuilder();
						continue;
					}
					if (c == ')' && !q) {

						String string = bld.toString();
						if (string.contains("Tanks_of_Finland")){
							System.out.println("A");
						}
						String[] split = string.split(",'");
						if (split.length == 0) {
							System.out.println("AA");
							continue;
						}
						ArrayList<String> sm = new ArrayList<String>();
						for (String qa : split) {
							if (qa.length() > 0) {
								sm.add(qa);
							}
						}
						split = sm.toArray(new String[sm.size()]);
						if (split.length != 7) {
							ecount++;
							System.out.println(sm);
							continue;
						}
//						if (true) {
//							//System.out.println(bld);
//							continue;
//						}
						for (int a = 0; a < split.length; a++) {
							if (split[a].length() > 0) {
								if (split[a].charAt(split[a].length() - 1) == '\'') {
									split[a] = split[a].substring(0,
											split[a].length() - 1);
								}
							} else {
								System.out.println("C");
							}
						}
						String parent = split[1];
						String child = split[2];
						
						boolean isUpper = split[5].equals("uppercase");
						boolean relation = split[6].equals("subcat");
						try {
							Integer m = Integer.parseInt(split[0]);

							if (m.intValue() == 0 && !relation) {
								ecount++;
								continue;
							}
							
							if (s.map.containsKey(m) || relation) {
								// System.out.println(parent+":"+child);
								pcount++;
								if (child.toLowerCase().equals("ships")){
									System.out.println("A");
								}
//								if (pcount % 1000 == 0) {
//									System.out.println(pcount + ":" + ecount
//											+ ":"
//											+ (((double) ecount) / pcount));
//								}
								ra.encode(parent, child, s.map.get(m),
										relation, isUpper);
							} else {
								ecount++;
								// System.err.println(parent+":"+child);
							}
							if (split.length == 1) {
								System.out.println("A");
							}
						} catch (Exception e) {
							continue;
						}
						continue;
					}
					if (c=='\\'){
						escapeCount++;
					}
					
					if (c == '\'') {
						if (q) {
							if (escapeCount%2==0) {
								if ((pc != '\'')||(ppc=='\\')) {									
									q = !q;
								} else {
									if (ppc==','){
										q=!q;
									}
									else{
										System.out.println(bld.toString());
									}
								}
							}
						} else {
							q = !q;
						}
					}
					if (c!='\\'){
						escapeCount=0;
					}
					bld.append(c);
					ppc = pc;
					pc = c;
				}
			}
			cs.tb.trim();
			ObjectOutputStream z = new ObjectOutputStream(
					new BufferedOutputStream(new FileOutputStream(new File(
							file, "content.cat"))));
			z.writeObject(cs.tb);
			z.close();
			ll.close();
			System.out.println(cs.tb.usedCount + ":" + usedCount);
			// ss.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
